import logging
# define custom log levels
logging.DEBUG_01 = 9
logging.DEBUG_02 = 8

# use: object = getClass("package.filename.classname")
def getClass(cls ):
    parts = cls.split('.')
    module = ".".join(parts[:-1])
    print(module)
    m = __import__( module )
    for comp in parts[1:]:
        m = getattr(m, comp)            
    return m

# use: object = getTrigger('triggerName')
# load package "notifiers.`triggerNameStnNotifier`" and import class "triggerNameStnNotifier"
def getNotifier(name):
    fileName = name + "StnNotifier"
    package = "notifiers." + fileName
    module = __import__( package )
    module = getattr(module, fileName)
    module = getattr(module, fileName)
    
    return module

def getTrigger(name):
    fileName = name + "StnTrigger"
    package = "triggers." + fileName
    
    module = __import__( package )
    module = getattr(module, fileName)
    module = getattr(module, fileName)
    
    return module

import html.parser
def htmlEntityDecode(htmlSource):
    h = html.parser.HTMLParser()
    return h.unescape(htmlSource)

from urllib.request import Request, urlopen, URLError
def getPage(url, user_agent="Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0"):
    req = Request(url=url, headers={'User-agent': user_agent})
    try:
        f = urlopen(req)
        htmlPage = f.read().decode('utf-8', 'ignore')
    except URLError as e:
        return False
        
    f.close()
    return htmlPage